//
//  HomeViewController.swift
//  CWLiving
//
//  Created by 方正刚 on 2017/7/5.
//  Copyright © 2017年 方正刚. All rights reserved.
//

import UIKit

private let PAGETITLEVIEW_HEIGHT: CGFloat = 40
let contentHeight = SCREEN_HEIGHT - NAV_HEIGHT - PAGETITLEVIEW_HEIGHT - TABBAR_HEIGHT
class HomeViewController: UIViewController {
    
    
    /// 懒加载闭包函数
    lazy var pageTitleView : PageTileView = { [weak self] in
        let titleFrame = CGRect(x: 0, y: NAV_HEIGHT, width: SCREEN_WIDHT , height: PAGETITLEVIEW_HEIGHT)
        let titles = ["推荐", "游戏", "娱乐", "趣玩"]
        let titleView = PageTileView(frame: titleFrame, titles: titles)
        
        titleView.delegate = self
        
        return titleView
    }()
    
    
    lazy var pageContentView : PageContentView = { [weak self] in
        
        let contentFrame = CGRect(x: 0, y: NAV_HEIGHT + PAGETITLEVIEW_HEIGHT, width: SCREEN_WIDHT, height: contentHeight)
        
        var childVCs = [UIViewController]()
        
        childVCs.append(RecommendViewController())
        childVCs.append(GameViewController())
        childVCs.append(AmusementViewController())
        childVCs.append(InterestingViewController())

    let contentView = PageContentView(frame: contentFrame, childVCs: childVCs, parentVC: self)
        contentView.delegate = self
        return contentView
    }()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    //设置界面
    setupUI()
        
    }

    

}


// MARK: - 设置UI
extension HomeViewController {
    
    func setupUI() {
        
        // 不需要调整UIScrollView的内边距
        automaticallyAdjustsScrollViewInsets = false
        
        //设置导航条
        setNavigationBar()
        
        //添加titleView
        view.addSubview(pageTitleView)
        
        //添加contentView
        view.addSubview(pageContentView)
    }
    
    /// 设置导航条方法
    private func setNavigationBar() {
        
        /// 导航条左侧的logo
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "logo")
        
        /// 设置右侧的Items
        let itemSize = CGSize(width: 40, height: 40)
        
        let histotyItem = UIBarButtonItem(imageName: "image_my_history", highImageName: "Image_my_history_click", size: itemSize)
   
        let searchItem = UIBarButtonItem(imageName: "btn_search", highImageName: "btn_search_clicked", size: itemSize)
       
        let qrcodeItem = UIBarButtonItem(imageName: "Image_scan", highImageName: "Image_scan_click", size: itemSize)
        
        navigationItem.rightBarButtonItems = [histotyItem, searchItem, qrcodeItem]
        
    }
    
}


// MARK: - PageTitleView代理协议
extension HomeViewController: PageTileViewDelegate {

    func pagetitleView(tittleVeiw: PageTileView, selectIndex: Int) {
        pageContentView.setCurrentIndex(currentIndex: selectIndex)
    }
    
}

// MARK: - PageContentView代理协议
extension HomeViewController: PageContentViewDelegate {
    func pageContentView(contentView: PageContentView, progress: CGFloat, targetIndex: Int, sourceIndex: Int) {
       pageTitleView.setTitleWithProgress(progress: progress, targetIndex: targetIndex, sourceIndex: sourceIndex)
    }
}

   
